package com.zzy.ecommerce.web;

import com.zzy.ecommerce.domain.TGoods;
import com.zzy.ecommerce.domain.TUser;
import com.zzy.ecommerce.service.GoodsService;
import com.zzy.ecommerce.service.impl.AccountServiceImpl;
import com.zzy.ecommerce.service.impl.GoodsServiceImpl;
import com.zzy.ecommerce.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private AccountServiceImpl accountService;
    @PostMapping("/login")
    public String userhomepage(String username, String password, Model model,HttpSession session){
        Integer oneUser = userService.selectOneUser(username, password);
        if (oneUser!=null){
            session.setAttribute("login",Thread.currentThread());
            session.setAttribute("user",username);
            model.addAttribute("userlogin",oneUser);
            return "redirect:/user/userhomepage";
        }else {
            model.addAttribute("login","账号密码不存在");
            return "index";
        }
    }


    @PostMapping("/register")
    public String register(TUser tUser, Model model){
        TUser tUser1 = userService.selectOneUserByUserName(tUser.getUsername());
        if (tUser1==null){
            int oneUser = userService.insertOneUser(tUser);
            TUser tUser2 = userService.selectOneUserByUserName(tUser.getUsername());
            Integer integer = accountService.insertOneAccountByUser(tUser2);
            model.addAttribute("msg","注册成功");
            return "index";
        }else {
            model.addAttribute("msg","注册失败 账号已存在");
            return "index";
        }
    }

    @GetMapping("/admin/adminacount")
    public String adminaccount(String username,String password, Model model,HttpSession session){
        if (username.equals("admin")&&password.equals("333")){
            session.setAttribute("msg","111");
            return "/admin/adminacount";
        }else {
            model.addAttribute("msg","账号密码错误");
            return "/admin/adminhomepage";
        }
    }
}
